package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import control.PDMLink;

import data.PDMNode;
import data.PDMStructure;

public class BaseFrame extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6563699293869441470L;
	private DefaultListModel nodes;
	//private PDMStructure struct;
	private JList nodeListPanel;
	
	public BaseFrame()
	{
		JMenuBar menu = buildMenu();
		nodes = new DefaultListModel();
		this.setBounds(50, 50, 300, 200);
		nodeListPanel = new JList(nodes);
		JScrollPane scroller = new JScrollPane(nodeListPanel);
		nodeListPanel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		nodeListPanel.setVisibleRowCount(-1);
		
		this.setLayout(new BorderLayout());
		
		this.add(menu, BorderLayout.NORTH);
		this.add(scroller);
		
		
	}
	
	public void addNode(PDMNode n)
	{
		nodes.addElement(n);
		nodeListPanel.ensureIndexIsVisible(nodes.size() - 1);
	}
	
	public PDMNode getNode(String key)
	{
		int numNodes = nodes.size();
		for(int i = 0; i < numNodes; i++)
		{
			PDMNode n = (PDMNode) nodes.getElementAt(i);
			if(n.getNodeKey().equals(key))
				return n;
		}
		return null;
	}
	
	private JMenuBar buildMenu()
	{
		JMenuBar menu = new JMenuBar();
		JMenu filemenu = new JMenu("File");
		JMenu nodeStuff = new JMenu("Edit");
		JMenu run = new JMenu("Run");
		
		JMenuItem addTask = new JMenuItem("Add Task...");
		addTask.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JDialog buildTask = new TaskEditor(BaseFrame.this);
				buildTask.setVisible(true);
			}
		});
		
		JMenuItem clearTasks = new JMenuItem("Clear Tasks");
		clearTasks.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				BaseFrame.this.nodes.clear();
			}
		});
		
		JMenuItem close = new JMenuItem("Close");
		close.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				BaseFrame.this.dispose();
				System.exit(0);
			}
		});
		
		JMenuItem buildGraph = new JMenuItem("Build Graph");
		buildGraph.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				LinkedHashMap<String, PDMNode> list = new LinkedHashMap<String, PDMNode>();
				GraphPlane gp = new GraphPlane();
				gp.setBackground(Color.WHITE);
				gp.addMouseMotionListener(gp);
				gp.addMouseListener(gp);
				int numNodes = nodes.size();
				for(int i = 0; i < numNodes; i++)
				{
					PDMNode p = (PDMNode) nodes.get(i);
					list.put(p.getNodeKey(), p);
					gp.add(p);
				}
				PDMLink.initForward(list);
				PDMLink.setNumbersForward(list);
				PDMLink.initBackward(list);
				PDMLink.setNumbersBackward(list);
				
				PDMLink critPF = new PDMLink();
				ArrayList<ArrayList<PDMNode>> crit = critPF.getCriticalPath(list);
				gp.updatePositions();
				gp.highlightCriticalPath(crit);
				JDialog disp = new JDialog(BaseFrame.this, true);
				disp.setLayout(new BorderLayout());
				JLabel instructions = new JLabel("Click and Drag Nodes to make clear.");
				instructions.setBackground(Color.GRAY);
				disp.setBounds(BaseFrame.this.getX() + 50, BaseFrame.this.getY() + 50, 600, 400);
				disp.add(gp);
				disp.add(instructions, BorderLayout.NORTH);
				disp.setVisible(true);
			}
		});

		filemenu.addSeparator();
		filemenu.add(close);
		nodeStuff.add(addTask);
		nodeStuff.add(clearTasks);
		run.add(buildGraph);
		
		menu.add(filemenu);
		menu.add(nodeStuff);
		menu.add(run);
		
		return menu;
	}
}
